package honest.playmusic.util;

import honest.playmusic.bean.Music;
import honest.playmusic.data.ConfigData;
import honest.playmusic.impl.PlayerImpl;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.TreeMap;

import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;

/**
 * 采用JL插件的播放器
 * @author Honest
 *
 */
public class MusicPlayerManager implements PlayerImpl {
	private Music music;
	private static Player player=null;   
	private static boolean lrcShowFlag=true;
	private static boolean lrcThreadEndFlag=false;
	private static Thread thread=null;
	private LocalLrcManager myLrc=new LocalLrcManager();
	TreeMap<Integer,String> treeMap=new TreeMap<Integer,String>();

	/**
	 * 初始话播放器
	 * @param path 需要播放歌曲路径
	 */
	public MusicPlayerManager(Music music){
		this.music=music;
		try {
			player=new Player(new FileInputStream(music.getMusicAbsolutePath()));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (JavaLayerException e) {
			e.printStackTrace();
		}
	} 


	/**
	 * 从头开始播放歌曲
	 */
	@Override
	public void playMusic(){
		//启动播放进程
		new Thread(){
			public void run(){
				try {
					player.play();
				} catch (JavaLayerException e) {
					e.printStackTrace();
				}
			}
		}.start();

		//启动歌词进程
		if(lrcShowFlag)
			startLrcThread();
	}

	/**
	 * 启动歌词线程
	 */
	private void startLrcThread(){
		//关闭先前歌词县城
		if(thread!=null&&lrcShowFlag){
			lrcThreadEndFlag=true;
			while(thread!=null){
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}

		lrcThreadEndFlag=false;
		
		thread=new Thread(){
			public void run(){
				treeMap.clear();
				treeMap=myLrc.getLrc(music);

				int playTime;
				if(treeMap.isEmpty()){
					System.out.println("歌词不存在");
					thread=null;
					return ;
				}
				while(lrcShowFlag&&!lrcThreadEndFlag){
					playTime=player.getPosition();
					int lrcTime;
					while(!treeMap.isEmpty()&&!lrcThreadEndFlag){
						lrcTime=treeMap.firstKey();
						if(lrcTime<playTime&&lrcShowFlag){
							System.out.println(FormatString.getFormatString2(treeMap.get(lrcTime),ConfigData.SHOW_WIDTH,true));
							treeMap.remove(lrcTime);
						}
						else
							break;
					}
					try {
						Thread.sleep(200);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				thread=null;
			}
		};

		//启动歌词显示
		thread.start();
	}

	/**
	 * 从指定位置开始播放歌曲
	 */
	@Override
	public void playMusic(int start, int end) {
		//JLPlayer暂时不提供直接支持
	}

	/**
	 * 停止播放歌曲
	 */
	@Override
	public int pauseMusic() {
		int frame=getCurrentFrame();
		player.close();
		player=null;
		return frame;
	}

	/**
	 * 停止播放音乐
	 */
	@Override
	public void stopMusic() {
		if(player!=null){
			player.close();
			player=null;
			lrcThreadEndFlag=true;
		}
		treeMap.clear();
	}

	/**
	 * 得到当前播放帧
	 */
	@Override
	public int getCurrentFrame() {
		return player.getPosition()/26;
	}

	/**
	 * 是否正在播放
	 */
	@Override
	public boolean isComplete() {
		return player.isComplete();
	}


	/**
	 * 设置歌词是否显示
	 * @param flag
	 */
	public void setLrcShow(boolean flag){
		lrcShowFlag=flag;	
	}

	/**
	 * 充重置歌词
	 */
	public void resetLrc(){
		startLrcThread();
	}

	/**
	 * 显示歌词
	 */


	@Override
	public void showLrc() {
		setLrcShow(true);
		startLrcThread();
	}

	/**
	 * 不显示歌词
	 */


	@Override
	public void unShowLrc() {
		setLrcShow(false);
		lrcThreadEndFlag=false;
	}
}
